package com.huasoft.servlet.file;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huasoft.dao.FileDao;
import com.huasoft.domain.File;

public class StatisticsFilesServlet {

	public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("aaaaaaaaaa");
		String conditions = request.getParameter("conditions");
		FileDao fileDao = new FileDao();
		List<File> files = fileDao.findAll();
		if (conditions.equals("types")) {
			Integer getFilesNumber = 0;
			Integer postFilesNumber = 0;
			for (File file : files) {
				if (file.getType().equals("发文")) {
					postFilesNumber++;
				} else {
					getFilesNumber++;
				}
			}
			System.out.println(postFilesNumber);
			request.setAttribute("getFilesNumber", getFilesNumber);
			request.setAttribute("postFilesNumber", postFilesNumber);
		} else if (conditions.equals("levels")) {
			Integer publicFilesNumber = 0;
			Integer secretFilesNumber = 0;
			Integer privateFilesNumber = 0;
			for (File file : files) {
				if (file.getLevel().equals("公开")) {
					publicFilesNumber++;
				} else if (file.getLevel().equals("秘密")) {
					secretFilesNumber++;
				} else {
					privateFilesNumber++;
				}
			}
			request.setAttribute("publicFilesNumber", publicFilesNumber);
			request.setAttribute("secretFilesNumber", secretFilesNumber);
			request.setAttribute("privateFilesNumber", privateFilesNumber);
		} else if (conditions.equals("status")) {
			Integer canBorrowFilesNumber = 0;
			Integer checkedFilesNumber = 0;
			Integer borrowedFilesNumber = 0;
			for (File file : files) {
				if (file.getStatus().equals("可借阅")) {
					canBorrowFilesNumber++;
				} else if (file.getStatus().equals("审核中")) {
					checkedFilesNumber++;
				} else {
					borrowedFilesNumber++;
				}
			}
			request.setAttribute("canBorrowFilesNumber", canBorrowFilesNumber);
			request.setAttribute("checkedFilesNumber", checkedFilesNumber);
			request.setAttribute("borrowedFilesNumber", borrowedFilesNumber);
		}
		request.getRequestDispatcher("/files/files_total.jsp").forward(request,
				response);
	}

}
